import { Service } from 'egg';
import * as moment from 'moment';

export default class extends Service {
    public async getTeacherSchedules(teacherIds: number[]) {
        const teachers = await this.service.db.teacherSchedule.getTeacherSchdules(teacherIds);
        const result: any[] = [];
        let tId = -1;
        let tTeacher: any = null;
        let tDay = -1;
        let tSchedule: any = null;

        teachers.forEach((teacher: any) => {
            if (tId !== teacher.teacher_id) {
                tId = teacher.teacher_id;
                tTeacher = {
                    teacher_id: teacher.teacher_id,
                    teacher_name: teacher.teacher_name,
                    schedules: [],
                };
                tDay = -1;
                result.push(tTeacher);
            }
            if (!teacher.day) {
                return;
            }
            if (tDay !== teacher.day) {
                tSchedule = {
                    day: teacher.day,
                    time_spans: [],
                };
                tDay = teacher.day;
                tTeacher.schedules.push(tSchedule);
            }
            tSchedule.time_spans.push({
                time_from: moment(teacher.time_from, 'HH:mm:SS').format('HH:mm'),
                time_to: moment(teacher.time_to, 'HH:mm:SS').format('HH:mm'),
            });
        });
        return result;
    }
}
